package zpe.service;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.sun.corba.se.spi.legacy.connection.GetEndPointInfoAgainException;

import zpe.genericcrud.service.GenericService;
import zpe.model.Carga;
import zpe.model.Conteiner;
import zpe.model.Enderecamento;
import zpe.model.ItemCarga;

@Service(value="enderecamentoService")
public class EnderecamentoService extends GenericService<Enderecamento> {
	
	private static final long serialVersionUID = 1L;
	
	@Autowired LocalService serviceLocal;
	@Autowired EnderecamentoHistoricoService serviceEnderecamentoHistorico;
	@Autowired @Qualifier("genericService") GenericService<Carga> serviceCarga;
	@Autowired @Qualifier("genericService") GenericService<Conteiner> serviceConteiner;
	@Autowired @Qualifier("genericService") GenericService<ItemCarga> serviceItemCarga;
	
	@SuppressWarnings("unchecked")
	public Enderecamento findByCarga( Carga carga ){
		DetachedCriteria c  = DetachedCriteria.forClass( Enderecamento.class )
			.createAlias("historico", "h", Criteria.INNER_JOIN)
			.createAlias("h.enderecamento", "e", Criteria.INNER_JOIN)
			.createAlias("e.carga", "c2", Criteria.INNER_JOIN)
			.createAlias("carga", "c", Criteria.INNER_JOIN)
			.add( Restrictions.eq( "c2.id", carga.getId() ) )
			.add( Restrictions.eq( "c.id", carga.getId() ) );

		
		List<Enderecamento> l = getDao().getEntityManager().findByCriteria( c );
		
		for(int i=0; i<l.size(); i++){
			l.get(i).setHistorico(getServiceEnderecamentoHistorico().carregaHistorico(l.get(i).getHistorico()));
		}
		
		if(l!=null && l.size()>0)
			return l.get(0);
		
		//if(l!=null && !l.isEmpty())
		//	return (Enderecamento) getDao().getEntityManager().findByCriteria( c ).get(0);
		
		return new Enderecamento();
	}
	/*
	@SuppressWarnings("unchecked")
	public List<EnderecamentoHistorico> findHistorico(Enderecamento end) {
		
		List<EnderecamentoHistorico> result = null;

		result = getDao().getEntityManager().find("from EnderecamentoHistorico h "
				+ "join Enderecamento e on (e.id = h.enderecamento.id)"
				+ "where h.enderecamento.id = "+end.getId()+" order by h.dataMovimentacao");
		
		
		Hibernate.initialize(end.getHistorico());
		
		return end.getHistorico();
	}
	*/

	public void setServiceCarga(CargaService serviceCarga) {
		this.serviceCarga = serviceCarga;
	}

	public GenericService<Conteiner> getServiceConteiner() {
		return serviceConteiner;
	}

	public void setServiceConteiner(GenericService<Conteiner> serviceConteiner) {
		this.serviceConteiner = serviceConteiner;
	}

	public GenericService<ItemCarga> getServiceItemCarga() {
		return serviceItemCarga;
	}

	public void setServiceItemCarga(GenericService<ItemCarga> serviceItemCarga) {
		this.serviceItemCarga = serviceItemCarga;
	}

	public LocalService getServiceLocal() {
		return serviceLocal;
	}

	public void setServiceLocal(LocalService serviceLocal) {
		this.serviceLocal = serviceLocal;
	}

	public GenericService<Carga> getServiceCarga() {
		return serviceCarga;
	}

	public void setServiceCarga(GenericService<Carga> serviceCarga) {
		this.serviceCarga = serviceCarga;
	}

	public EnderecamentoHistoricoService getServiceEnderecamentoHistorico() {
		return serviceEnderecamentoHistorico;
	}

	public void setServiceEnderecamentoHistorico(
			EnderecamentoHistoricoService serviceEnderecamentoHistorico) {
		this.serviceEnderecamentoHistorico = serviceEnderecamentoHistorico;
	}

}
